<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>登录</title>
		<link rel="stylesheet" type="text/css" href="css/public.css"/>
		<link rel="stylesheet" type="text/css" href="X-admin/css/login.css"/>
		<script src="X-admin/js/jquery.js"></script>
		<script src="X-admin/lib/layui/layui.js"></script>
		<script src="js/checkAutoLogin.js"></script>

	</head>
	<body>
		<!-------------------login-------------------------->
		<div class="login">
			<form action="http://localhost:9200/user/updatePassword" method="post">
				<h1><a href="X-admin/index.html"><img src="img/logo1.png" style="height: 80px;width: 160px"></a></h1>
				<p></p>
				<div class="msg-warn hide"><b></b>公共场所不建议自动登录，以防账号丢失</div>
				<p><input type="text"  id="username" name="" value="1559541538" placeholder="昵称/邮箱/手机号"></p>
				<p><input type="text" id="passwd" name="" value="123456" placeholder="密码"></p>
				<div class="code">
					<div >
						<input name="code" lay-verify="code" value="0" placeholder="验证码" type="text" class="layui-input"
							   id="code">
					</div>
						<img src="http://localhost:9200/api-lessors/code/getCode" id="code_img">
				</div>
				<p><input type="submit"  lay-submit lay-filter="login" name="" value="登  录"></p>
				<p class="txt"><a href="forget.html">忘记密码？</a></p>
				<p class="txt"><a class="" href="lessors-register.html">注册</a></p>
				<p><input type="radio" name="autoLogin" value="2">自动登录</p>
			</form>
		</div>

	</body>
	<script>

		var COOKIE_NAME_AUTOLOGIN = "autoLogin";
		var COOKIE_NAME = "universe";
		checkRadio();  //检查单选框
		function checkRadio() {
			if (document.cookie.indexOf(COOKIE_NAME_AUTOLOGIN) !== -1) {
				$("input[name='autoLogin']").attr("checked", 'checked')
			}
		}

		cookieStart(); //单选框点击切换状态
		function cookieStart() {
			var old = null; //用来保存原来的对象
			$("input[name='autoLogin']").each(function () {//循环绑定事件
				if (this.checked) {
					old = this; //如果当前对象选中，保存该对象
					console.log(old.value)
					// addCookie(COOKIE_NAME, old.value, 60)
				}
				this.onclick = function () {
					if (this === old) {//如果点击的对象原来是选中的，取消选中
						this.checked = false;
						old = null;
						DelCookie(COOKIE_NAME_AUTOLOGIN);
					} else {
						old = this;
						addCookie(COOKIE_NAME_AUTOLOGIN, old.value);
					}
				}
			});
		}

		//写Cookie-->判断是否启用自动登录
		function addCookie(objName, objValue) {
			var str = objName + "=" + objValue;  // 设置cookie的value
			// if (time > 0) {//设置cookie存活时间
			var date = new Date();
			// var ms = objHours * 3600 * 1000;
			var ms = 3600 * 1000*24*365; //设置记住自动登录状态为时间为一年
			date.setTime(date.getTime() + ms);
			str += "; expires=" + date.toGMTString();

			document.cookie = str;
		}
		//写cookie-->登录成功后设置保存用户信息的cookie
		function addCookieForUser(username, userid, avatar, time) {
			if (document.cookie.indexOf(COOKIE_NAME_AUTOLOGIN) !== -1) { //检查是否启用了自动登录
				console.log(userid);
				var str = COOKIE_NAME + "=" + username  + ":" + userid + ":" + avatar;
				if (time > 0) {//设置cookie存活时间
					var date = new Date();
					// var ms = objHours * 3600 * 1000;
					var ms = time * 3600 * 1000; //暂时设置保存用户信息cookie为7天，7天内实现自动登录
					date.setTime(date.getTime() + ms);
					str += "; expires=" + date.toGMTString();
				}
				console.log(str);
				document.cookie = str;
			}
		}

		//清除cookie,将过期时间改为-1 即可
		function DelCookie(name) {
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			document.cookie = name + "=" + 2 + "; expires=" + exp.toGMTString();
		}

		$(function () {
			layui.use('form', function () {
				var form = layui.form;
				form.on('submit(login)', function () {
					var field = {
						userna: $("#username").val(),
						passwd: $("#passwd").val(),
						locode: $("#code").val()
					};
					$.ajax({
						url: 'http://localhost:9200/api-lessors/login/loginCheck',//地址：访问后台的地址
						data: field,//前端传递给后台的数据
						type: 'post',
						async: false,
						xhrFields: {
							withCredentials: true
						},
						crossDomain: true,
						dataType: 'json',//是后端传递到前端的数据格式
						success: function (data) {//后端给前端的一个反馈
							console.log(data.data);
							if (data.msg === "验证码不正确！") {
								layer.msg("验证码不正确！");
							}
							if (data.msg === "密码错误！") {
								layer.msg("密码错误！");
							}
							if (data.msg === "用户不存在或被禁用！") {
								layer.msg("用户不存在或被禁用！");
							}
							if (data.code === 0) {
								layer.msg("登录成功！", function () {
									sessionStorage.setItem("busiid",data.data.busiid);
									// sessionStorage.setItem("userid",data.data.busiid);
									sessionStorage.setItem("buhead",data.data.buhead);
									sessionStorage.setItem("userType","business");
									console.log(sessionStorage.getItem("busiid"));
									addCookieForUser(data.data.buname, data.data.busiid, data.data.buhead, 7 * 24);
									window.open("index.html", "_self");
								});
							}
						}
					});
					return false;
				});
			});
			$('#code_img').click(function (e) {
				$("#code_img").attr('src', "http://localhost:9200/api-lessors/code/getCode?random=" + Math.random());
			});
		})
	</script>
</html>
